package com.dmooo.ymx.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dmooo.ymx.R;
import com.dmooo.ymx.bean.ShopMallZhujibean;
import com.dmooo.ymx.bean.Zhujibean;
import com.dmooo.ymx.common.SPUtils;
import com.dmooo.ymx.config.Constants;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 */

public class TodayHighlightsChildAdapter4 extends RecyclerView.Adapter<TodayHighlightsChildAdapter4.MyHolder> {

    private Context context;
    private List<ShopMallZhujibean> lists;
    private SubClickListener subClickListener;


    public interface OnremoveListnner{
        void ondelect(int i);
    }
    private OnremoveListnner onremoveListnner;
    public void setOnremoveListnner(OnremoveListnner onremoveListnner){
        this.onremoveListnner = onremoveListnner;
    }


    DecimalFormat df=new DecimalFormat("0.00");
    public TodayHighlightsChildAdapter4(Context context, List<ShopMallZhujibean> list) {
        this.context = context;
        this.lists = list;
    }

    public void setsubClickListener(SubClickListener topicClickListener) {
        this.subClickListener = topicClickListener;
    }

    public interface SubClickListener {
        void OntopicClickListener(View v, String detail, int posit);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        Glide.with( context ).load(Constants.APP_IP+ lists.get( position ).img ).into( holder.iv_img );
        holder.tv_title.setText( lists.get( position ).goods_name );
//        holder.tv_ygs.setText( "预估省:"+df.format((Double.parseDouble( lists.get( position ).zk_final_price ) - Double.parseDouble( lists.get( position ).coupon_amount ))*Double.parseDouble( lists.get( position ).commission_rate )/100* Double.parseDouble(df.format((float) SPUtils.getIntData(context, "rate", 0) / 100))+ Double.parseDouble( lists.get( position ).coupon_amount ) ) );
//        holder.tv_quan.setText( lists.get( position ).coupon_amount );
        holder.tv_qhj.setText( "￥"+lists.get(position).old_price);
        holder.tv_yj.setText( lists.get( position ).price );
        holder.tv_yj.getPaint().setFlags( Paint.STRIKE_THRU_TEXT_FLAG);
//        holder.tv_xiaoliang.setText( "已售:"+lists.get( position ).volume );
        holder.itemView.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (subClickListener!=null){
                    subClickListener.OntopicClickListener( view,"item",position );
                }
            }
        } );

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onremoveListnner!=null){
                    onremoveListnner.ondelect(position);
                }
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyHolder myHolder = null;
        View view = LayoutInflater.from( parent.getContext() ).inflate( R.layout.shopmall_highlights_child_item, parent, false );
        myHolder = new MyHolder( view );
        return myHolder;
    }

    public static String timeStamp2Date(long time, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(time));
    }

    //初始化控件
    class MyHolder extends RecyclerView.ViewHolder{

        private TextView hehe_jiage,tv_title,tv_ygs,tv_quan,tv_qhj,tv_yj,tv_xiaoliang;
        private ImageView iv_img;

        public MyHolder(View itemView) {
            super( itemView );
            iv_img = itemView.findViewById( R.id.image );
            tv_title = itemView.findViewById( R.id.title_child );
            tv_ygs = itemView.findViewById( R.id.tx4 );
            tv_quan = itemView.findViewById( R.id.tx3 );
            tv_qhj = itemView.findViewById( R.id.tx2 );
            tv_yj = itemView.findViewById( R.id.tx2_2 );
            tv_xiaoliang = itemView.findViewById( R.id.tx5 );


        }
    }
}
